package com.youlu.campus.admin.community.controller;

import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.ActivityPlatformConfig;
import com.youlu.campus.entity.VO.req.ActivityPlatformConfigQueryVO;
import com.youlu.campus.service.activity.ActivityPlatformConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;

@RestController
public class ActivityPlatformConfigController {
    @Autowired
    private ActivityPlatformConfigService activityPlatformConfigService;

    @PostMapping("/activity/platform-config/list")
    public RestResult list(@RequestBody @Valid ActivityPlatformConfigQueryVO req) {
        String comId = AuthUtils.getComId();
        Page<ActivityPlatformConfig> users = activityPlatformConfigService.list(req);
        if (users == null) {
            PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
            users = PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0L);
        }
        return RestResultBuilder.builder().success().data(users).build();
    }

    @PostMapping("/activity/platform-config/create")
    public RestResult create(@RequestBody @Valid ActivityPlatformConfig req) {
        String comId = AuthUtils.getComId();
        req.setOptUserLoginName(AuthUtils.getLoginName());
        boolean re = activityPlatformConfigService.create(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().success().failure().build();
    }

    @PostMapping("/activity/platform-config/update")
    public RestResult update(@RequestBody @Valid ActivityPlatformConfig req) {
        String comId = AuthUtils.getComId();
        req.setOptUserLoginName(AuthUtils.getLoginName());
        boolean re = activityPlatformConfigService.update(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().success().failure().build();
    }

    @GetMapping("/activity/platform-config/find-one")
    public RestResult findOne(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        ActivityPlatformConfig re = activityPlatformConfigService.findOne(id);
        return RestResultBuilder.builder().data(re).build();
    }

    @GetMapping("/activity/platform-config/delete")
    public RestResult delete(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        boolean re = activityPlatformConfigService.delete(id, AuthUtils.getLoginName());
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().success().failure().build();
    }

    @GetMapping("/activity/platform/find")
    public RestResult findAppId(@RequestParam("platform") String platform) {
        String comId = AuthUtils.getComId();
        return RestResultBuilder.builder().success(activityPlatformConfigService.findByPlatform(platform)).build();
    }
}
